//
//  SN_SetQuestionPwd_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/8/2.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_SetQuestionPwd_Controller.h"
#import "SN_ForgetAndEditPwd_Controller.h"

@interface SN_SetQuestionPwd_Controller () <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *field;
@end

@implementation SN_SetQuestionPwd_Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    

    UIImageView *iconImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"叹号"]];
    iconImgV.frame = CGRectMake(15, 15 + SafeAreaTopHeight, 15, 15);
    [self.view addSubview:iconImgV];
    
    NSString *titleStr;
    NSString *infoStr;
    NSString *navTitleStr;
    if (_type == QuestionPwdTypeInput) {
        titleStr = @"您已经离开一段时间,为确保信息安全,请输入解题密码";
        infoStr = @"解题密码";
        navTitleStr = @"输入解题密码";
    }else if (_type == QuestionPwdTypeSetting) {
        titleStr = @"设置解题密码可保护您的信息安全,保证解题资料不会被别人看到";
        infoStr = @"设置解题密码";
        navTitleStr = infoStr;
    }else {
        titleStr = @"请输入解题密码确认删除";
        infoStr = @"删除解题密码";
        navTitleStr = infoStr;
    }
    
    self.navBar.navigationItem.title = navTitleStr;
    UIBarButtonItem *confirmBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定 " style:UIBarButtonItemStyleDone target:self action:@selector(confirmAction:)];
    [confirmBtn setTintColor:[UIColor whiteColor]];
    self.navBar.navigationItem.rightBarButtonItem = confirmBtn;
    
    CGFloat width = VIEW_WIDTH - CGRectGetMaxX(iconImgV.frame) - 15;
    
    
    CGFloat height = [titleStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iconImgV.frame) + 2, 14 + SafeAreaTopHeight, width, height)];
    infoLabel.font = [UIFont systemFontOfSize:15];
    infoLabel.numberOfLines = 0;
    infoLabel.textColor = [UIColor colorWithHexString:@"#979797"];
    infoLabel.text = titleStr;
    [self.view addSubview:infoLabel];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(infoLabel.frame) + 10, VIEW_WIDTH, 45)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 45)];
    leftLabel.font = [UIFont systemFontOfSize:15];
    leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.text = infoStr;
    [view addSubview:leftLabel];
    
    _field = [[UITextField alloc] initWithFrame:CGRectMake(140, 0, VIEW_WIDTH - 140, 45)];
    _field.placeholder = @"输入四位数字";
    _field.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
    _field.keyboardType = UIKeyboardTypeNumberPad;
    _field.borderStyle = UITextBorderStyleNone;
    [view addSubview:_field];
    _field.delegate = self;
    _field.secureTextEntry = YES;
    _field.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:view];
    
    UIButton *forgetPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetPwdBtn setTitle:@"忘记解题密码" forState:UIControlStateNormal];
    [forgetPwdBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
    [forgetPwdBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateSelected];
    forgetPwdBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [forgetPwdBtn setFrame:CGRectMake(VIEW_WIDTH - 95 - 15, CGRectGetMaxY(view.frame) + 39, 95, 16)];
    [forgetPwdBtn addTarget:self action:@selector(forgetQuestionPwd) forControlEvents:UIControlEventTouchUpInside];
    forgetPwdBtn.hidden = YES;
    [self.view addSubview:forgetPwdBtn];
    
    
    UIButton *editPwdbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [editPwdbtn setTitle:@"修改解题密码" forState:UIControlStateNormal];
    [editPwdbtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
    [editPwdbtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateSelected];
    editPwdbtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [editPwdbtn setFrame:CGRectMake(VIEW_WIDTH - 95 - 15, CGRectGetMaxY(forgetPwdBtn.frame) + 16, 95, 16)];
    [editPwdbtn addTarget:self action:@selector(editQuestionPwd) forControlEvents:UIControlEventTouchUpInside];
    editPwdbtn.hidden = YES;
    [self.view addSubview:editPwdbtn];
    
    if (_type == QuestionPwdTypeInput) {
        editPwdbtn.hidden = NO;
        forgetPwdBtn.hidden = NO;
    }
        
}

//忘记密码
- (void)forgetQuestionPwd {

    SN_ForgetAndEditPwd_Controller *vc = [[SN_ForgetAndEditPwd_Controller alloc] init];
    vc.type = 1;
    [self.navigationController pushViewController:vc animated:YES];
}

//修改密码
- (void)editQuestionPwd {
    
    SN_ForgetAndEditPwd_Controller *vc = [[SN_ForgetAndEditPwd_Controller alloc] init];
    vc.type = 2;
    [self.navigationController pushViewController:vc animated:YES];
}

// 确定
- (void)confirmAction:(UIButton *)sender {
    
    if (_field.text.length != 4) {
        [Utils alertViewWithMessage:@"请输入四位解题密码"];
        return;
    }
    
    if (_type == QuestionPwdTypeInput) { //输入
        
    }else if (_type == QuestionPwdTypeSetting) { //设置
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:@"api_user_setszbpwd" forKey:@"method"];
        [param setObject:_field.text forKey:@"szbpwd"];
        [param setObject:USERID forKey:@"userid"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSString *title = [jsonObject objectForKey:@"re_info"];
            
            NSMutableDictionary *re_dataDict = [[SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID] mutableCopy];
        
            [re_dataDict setObject:_field.text forKey:@"szbpwd"];
            [SyUserInfomation personalImformationSaveWtih:re_dataDict];
            
            [self showHint:title];
            
            if (self.block) {
                
                self.block(1);
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        
        } failure:^(NSInteger recode, NSError *error) {
           
            [self showHint:error.localizedDescription];
        }];
    
    }else if (_type == QuestionPwdTypeDelete ) { //删除
    
        if (![_field.text isEqualToString:[SYAccountTool ShareAccount].account.szbpwd]) {
            
            [Utils alertViewWithMessage:@"密码输入错误,请重新输入"];
            return;
        }
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:@"api_user_updateszbpwd" forKey:@"method"];
        [param setObject:USERID forKey:@"userid"];
        [param setObject:@"0" forKey:@"type"];
        [param setObject:_field.text forKey:@"oldszbpwd"];
        [param setObject:@"0" forKey:@"szbpwd"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSString *str = @"删除解题密码成功";
            [self showHint:str];
            
            if (self.block) {
                
                self.block(0);
            }
            
            NSMutableDictionary *re_dataDict = [[SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID] mutableCopy];
            
            [re_dataDict removeObjectForKey:@"szbpwd"];
            [SyUserInfomation personalImformationSaveWtih:re_dataDict];
            
            [self.navigationController popViewControllerAnimated:YES];

            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];
        
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length != 0 && textField.text.length >= 4) {
        return NO;
    }
    
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

@end
